package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.example.entity.Candidateresume;
import org.example.models.R;
import org.example.service.CandidateresumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/candidateresume")
public class CandidateresumeController {
    @Autowired
    private CandidateresumeService candidateresumeService;

    // 查询
    @GetMapping("/find")
    public R<Page<Candidateresume>> find(
            @RequestParam(value = "current", defaultValue = "1") int current, // 当前页码
            @RequestParam(value = "size", defaultValue = "10") int size, // 每页记录数
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition, // 意向岗位
            @RequestParam(value = "resumeStatus", required = false) String resumeStatus // 简历状态
    ) {
        QueryWrapper<Candidateresume> queryWrapper = new QueryWrapper<>();// 创建查询条件

        if (intendedPosition != null && !intendedPosition.isEmpty()) {
            queryWrapper.like("intendedPosition", intendedPosition);// 模糊查询
        }
        Page<Candidateresume> page = new Page<>(current, size); // 创建分页对象
        return R.success(candidateresumeService.page(page, queryWrapper));
    }

    // 查询状态 为已入职
    @PostMapping("/findByStatus")
    public R<Page<Candidateresume>> findByStatus(
            @RequestParam(value = "current", defaultValue = "1") int current, // 当前页码
            @RequestParam(value = "size", defaultValue = "10") int size,     // 每页记录数
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition, // 意向岗位
            @RequestParam(value = "resumeStatus", required = false) String resumeStatus // 简历状态
    ) {

        QueryWrapper<Candidateresume> queryWrapper = new QueryWrapper<>();
        //  设置查询条件为已入职
        if (Objects.equals(resumeStatus, "已入职")) {
            queryWrapper.eq("Resume_status", "已入职");
        } else if (Objects.equals(resumeStatus, "待面试") || resumeStatus == null) { //  设置查询条件为待面试或者为空
            queryWrapper.eq("Resume_status", "待面试");

        } else if (Objects.equals(resumeStatus, "已淘汰")) {//  设置查询条件为已淘汰
            queryWrapper.eq("Resume_status", "已淘汰");
        }

        if (intendedPosition != null && !intendedPosition.isEmpty()) {
            queryWrapper.like("Intended_position", intendedPosition);

        }


        Page<Candidateresume> page = new Page<>(current, size);
        return R.success(candidateresumeService.page(page, queryWrapper));

    }


    //通过简历id获取简历信息
    @GetMapping("/getById")
    public R<Candidateresume> getById(@RequestParam("candidateid") String candidateid) {
        Candidateresume byId = candidateresumeService.getById(candidateid);
        return R.success(byId);
    }

    @GetMapping("/queryResumeAndInterviewRecordsBasedOnId")
    public R<List<Candidateresume>> queryResumeAndInterviewRecordsBasedOnId(@RequestParam("candidateid") String candidateid) {
        List<Candidateresume> candidateresumes = candidateresumeService.selectCandidateWithInterviewByCandidateId(candidateid);
        return R.success(candidateresumes);
    }

    // 添加
    @PostMapping("/insert")
    public R insert(@RequestBody Candidateresume candidateresume) {
        candidateresume.setFilterDate(new Date());
        if (candidateresume == null) {
            return R.error("参数错误");
        }
        //获取当前时间
        candidateresume.setDateofresumeaddition(new java.util.Date());
        boolean save = candidateresumeService.save(candidateresume);
        return R.success(200, "添加成功", save);
    }

    // 根据id删除
    @PostMapping("/deleteById")
    public R deleteById(@RequestParam("candidateid") Integer candidateid) {
        Candidateresume byId = candidateresumeService.getById(candidateid);
        return R.success(candidateresumeService.removeById(byId));
    }

    // 根据id查询
    @PostMapping("/findById")
    public R<Candidateresume> findById(@RequestParam("candidateid") Integer candidateid) {
        Candidateresume byId = candidateresumeService.getById(candidateid);
        return R.success(byId);
    }

    //根据简历添加日期升序
    @GetMapping("/sortByDate")
    public R<Page<Candidateresume>> sortByDate(
            @RequestParam(value = "current", defaultValue = "1") int current, // 当前页码
            @RequestParam(value = "size", defaultValue = "10") int size, // 每页记录数
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition, // 意向岗位
            @RequestParam(value = "resumeStatus", required = false) String resumeStatus // 简历状态
    ) {
        QueryWrapper<Candidateresume> queryWrapper = new QueryWrapper<>();// 创建查询条件
        if (Objects.equals(resumeStatus, "已入职")) {
            queryWrapper.eq("Resume_status", "已入职");
        } else if (Objects.equals(resumeStatus, "待面试") || resumeStatus == null) { //  设置查询条件为待面试或者为空
            queryWrapper.eq("Resume_status", "待面试");

        } else if (Objects.equals(resumeStatus, "已淘汰")) {//  设置查询条件为已淘汰
            queryWrapper.eq("Resume_status", "已淘汰");
        }
        if (intendedPosition != null && !intendedPosition.isEmpty()) {
            queryWrapper.like("intendedPosition", intendedPosition);
        }
        Page<Candidateresume> page = new Page<>(current, size);
        return R.success(candidateresumeService.page(page, queryWrapper.orderByAsc("dateofresumeaddition")));
    }

    //根据简历添加日期降序
    @GetMapping("/sortByDateDesc")
    public R<Page<Candidateresume>> sortByDateDesc(
            @RequestParam(value = "current", defaultValue = "1") int current, // 当前页码
            @RequestParam(value = "size", defaultValue = "10") int size, // 每页记录数
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition, // 意向岗位
            @RequestParam(value = "resumeStatus", required = false) String resumeStatus // 简历状态
    ) {
        QueryWrapper<Candidateresume> queryWrapper = new QueryWrapper<>();
        if (Objects.equals(resumeStatus, "已入职")) {
            queryWrapper.eq("Resume_status", "已入职");
        } else if (Objects.equals(resumeStatus, "待面试") || resumeStatus == null) { //  设置查询条件为待面试或者为空
            queryWrapper.eq("Resume_status", "待面试");

        } else if (Objects.equals(resumeStatus, "已淘汰")) {//  设置查询条件为已淘汰
            queryWrapper.eq("Resume_status", "已淘汰");
        }
        if (intendedPosition != null && !intendedPosition.isEmpty()) {
            queryWrapper.like("intendedPosition", intendedPosition);
        }
        Page<Candidateresume> page = new Page<>(current, size);
        return R.success(candidateresumeService.page(page, queryWrapper.orderByDesc("dateofresumeaddition")));
    }

    //查询状态为 待发offer
    @GetMapping("/checkOffer")
    public R<List<Candidateresume>> checkOffer() {
        QueryWrapper<Candidateresume> queryWrapper = new QueryWrapper<>(); // 创建查询条件
        queryWrapper.eq("Resume_status", "待发offer"); // 设置查询条件为待发offer
        return R.success(candidateresumeService.list(queryWrapper)); // 返回符合条件的记录
    }

    //查询查询状态为 待发offer
    @GetMapping("/queryStatus")
    public R<List<Candidateresume>> queryStatus() {
        List<Candidateresume> candidateresumes = candidateresumeService.resumeInquiryDepartment("待发offer");
        return R.success(candidateresumes); // 返回符合条件的记录
    }

    //根据简历id修改
    @PostMapping("/update")
    public R update(@RequestBody Candidateresume candidateresume) {
        boolean b = candidateresumeService.updateById(candidateresume);
        if (!b) {
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }

    //根据需求 状态 职位名称来查询
    @GetMapping("/queryBasedOnTheDemandStatusAndJobName")
    public R<List<Candidateresume>> queryBasedOnTheDemandStatusAndJobName(@RequestParam(value = "status", required = false) String status,
                                                                          @RequestParam(value = "jobName", required = false) String jobName,
                                                                          @RequestParam(value = "requirements", required = false) String requirements) {
        status = "待面试";

        List<Candidateresume> list = candidateresumeService.queryBasedOnTheDemandStatusAndJobName(status, jobName, requirements);
        if (list != null) {
            return R.success(list);
        }
        return R.success("查询错误");
    }

    //根据需求查询简历
    @GetMapping("/queryResumeRequirementsBasedOnStatus")
    public R<List<Candidateresume>> queryResumeRequirementsBasedOnStatus(@RequestParam(value = "status", required = false) String status,
                                                                         @RequestParam(value = "demandId", required = false) String demandId
    ) {
        List<Candidateresume> list = candidateresumeService.reviewTheResumeAsNeeded(status, demandId);
        return R.success(list);
    }

    //根据id修改简历状态
    @PostMapping("/updateResumeStatusById")
    public R updateResumeStatusById(@RequestParam(value = "resumeStatus", required = false) String resumeStatus,
                                    @RequestParam(value = "candidateid", required = false) String candidateid) {
        int i = candidateresumeService.updateResumeStatusById(resumeStatus, candidateid);
        if (i > 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    // 查询待发 Offer 候选人及其关联的部门薪酬标准
    @GetMapping("/selectCandidateResumesWithDeptStruct")
    public R<List<Candidateresume>> selectCandidateResumesWithDeptStruct(@Param("status") String status) {
        List<Candidateresume> candidateresumes = candidateresumeService.selectCandidateResumesWithDeptStruct(status);
        if (candidateresumes != null) {
            return R.success(candidateresumes);
        }
        return R.error("查询错误");
    }

    //招聘需求（Requirement）和部门结构 (带模糊查询名称和岗位)
    @GetMapping("/searchCandidateResumesByStatusAndNameOrPost")
    public R<List<Candidateresume>> searchCandidateResumesByStatusAndNameOrPost(@RequestParam(value = "status", required = false) String status,
                                                                                @RequestParam(value = "candidateName", required = false) String candidateName,
                                                                                @RequestParam(value = "postsName", required = false) String postsName,
                                                                                @RequestParam(value = "current", defaultValue = "1") int current,
                                                                                @RequestParam(value = "size", defaultValue = "10") int size) {
        status = "待发offer";
        List<Candidateresume> list = candidateresumeService.searchCandidateResumesByStatusAndNameOrPost(status, candidateName, postsName);
        int start = (current - 1) * size;
        int end = start + size;

        // 确保不会超出列表范围
        if (start > list.size()) {
            start = list.size();
        }
        if (end > list.size()) {
            end = list.size();
        }

        // 分页处理
        List<Candidateresume> pageList = list.subList(start, end);

        return R.success(pageList);
    }

    @GetMapping("/searchCandidateResumesByStatusAndNameOrPostASC")
    public R<List<Candidateresume>> searchCandidateResumesByStatusAndNameOrPostASC(@RequestParam(value = "status", required = false) String status,
                                                                                   @RequestParam(value = "candidateName", required = false) String candidateName,
                                                                                   @RequestParam(value = "postsName", required = false) String postsName,
                                                                                   @RequestParam(value = "current", defaultValue = "1") int current,
                                                                                   @RequestParam(value = "size", defaultValue = "10") int size) {
        status = "待发offer";
        List<Candidateresume> list = candidateresumeService.searchCandidateResumesByStatusAndNameOrPostASC(status, candidateName, postsName);
        int start = (current - 1) * size;
        int end = start + size;
        if (start > list.size()) {
            start = list.size();
        }
        if (end > list.size()) {
            end = list.size();
        }
        List<Candidateresume> pageList = list.subList(start, end);
        return R.success(pageList);
    }

    @GetMapping("/searchCandidateResumesByStatusAndNameOrPostDESC")
    public R<List<Candidateresume>> searchCandidateResumesByStatusAndNameOrPostDESC(@RequestParam(value = "status", required = false) String status,
                                                                                    @RequestParam(value = "candidateName", required = false) String candidateName,
                                                                                    @RequestParam(value = "postsName", required = false) String postsName,
                                                                                    @RequestParam(value = "current", defaultValue = "1") int current,
                                                                                    @RequestParam(value = "size", defaultValue = "10") int size) {

        status = "待发offer";
        List<Candidateresume> list = candidateresumeService.searchCandidateResumesByStatusAndNameOrPostDESC(status, candidateName, postsName);
        int start = (current - 1) * size;
        int end = start + size;
        if (start > list.size()) {
            start = list.size();
        }
        if (end > list.size()) {
            end = list.size();
        }
        List<Candidateresume> pageList = list.subList(start, end);
        return R.success(pageList);
    }

}
